package com.bigdata.service.service;

import com.amazonaws.util.Md5Utils;
import com.bigdata.common.constant.CacheConstants;
import com.bigdata.common.core.domain.model.RegisterBody;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.utils.redis.RedisUtils;
import com.bigdata.service.domain.bo.ServiceUsersBo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class AppRegisterService {
    @Autowired
    private IServiceUsersService serviceUsersService;
    /**
     * 注册
     */
    public Boolean register(ServiceUsersBo bo) {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(bo.getUuid(), "");
        String captcha = RedisUtils.getCacheObject(verifyKey);
        RedisUtils.deleteObject(verifyKey);
        if (StringUtils.isBlank(captcha)) {
            throw new RuntimeException("验证码已过期");
        }
        if (!bo.getCode().equalsIgnoreCase(captcha)) {
            throw new RuntimeException("验证码错误");
        }

        bo.setPasswordHash(Md5Utils.md5AsBase64(bo.getPasswordHash().getBytes()));
       return serviceUsersService.insertByBo(bo);
    }
}
